package com.blb.service;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.blb.dao.UserMapper;
import com.blb.entity.User;
import com.blb.utils.LayuiPage;
import com.blb.utils.MybatisUtils;

/**
 * 用户业务逻辑层
 * @author Administrator
 *
 */
public class UserService {

	private UserMapper userMapper;
	
	/**
	 * 根据用户名查询用户
	 * @param userName
	 * @return
	 * @throws SQLException
	 * @throws IOException 
	 */
	public User getUserByUserName(String userName) throws SQLException, IOException{
		SqlSession session = MybatisUtils.getSession();
		userMapper = session.getMapper(UserMapper.class);
		User user = userMapper.getUserByUserName(userName);
		MybatisUtils.closeSession(session);
		return user;
	}
	
	/**
	 * 添加用户
	 * @param user
	 * @throws SQLException
	 * @throws IOException 
	 */
	public void addUser(User user) throws SQLException, IOException{
		SqlSession session = MybatisUtils.getSession();
		userMapper = session.getMapper(UserMapper.class);
		userMapper.addUser(user);
		session.commit();
		MybatisUtils.closeSession(session);
	}
	
	/**
	 * 用户分页数据
	 * @param start
	 * @param end
	 * @return
	 * @throws SQLException
	 * @throws IOException 
	 */
	public LayuiPage page(int start, int end, String username, String job) throws SQLException, IOException{
		SqlSession session = MybatisUtils.getSession();
		userMapper = session.getMapper(UserMapper.class);
		List<User> list = userMapper.getUserPage(start, end, username, job);
		LayuiPage layuiPage = new LayuiPage();
		layuiPage.setData(list);
		layuiPage.setCount(userMapper.getCount(username, job));
		MybatisUtils.closeSession(session);
		return layuiPage;
	}
	
	/**
	 * 删除用户
	 * @param ids
	 * @throws IOException 
	 */
	public void delete(String[] ids) throws IOException{
		SqlSession session = MybatisUtils.getSession();
		userMapper = session.getMapper(UserMapper.class);
		userMapper.delete(ids);
		session.commit();
		MybatisUtils.closeSession(session);
	}
	
}
